package cn.jbolt.ec.spreader;

import cn.jbolt.base.JBoltBaseService;
import cn.jbolt.common.model.BecMember;
import cn.jbolt.common.model.BecSpreader;
import cn.jbolt.ec.member.MemberService;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

public class SpreaderService extends JBoltBaseService<BecSpreader> {
    @Override
    protected BecSpreader dao() {
        return new BecSpreader().dao();
    }

    @Inject
    MemberService memberService;

    /**
     * 申请成为分销商
     * @param member_id
     * @param name
     * @param mobile
     * @param why_apply
     * @return
     */
    Boolean applicant_spreader(Integer member_id, String name, String mobile, String why_apply) {
        BecSpreader becSpreader2 = new BecSpreader();
        BecSpreader becSpreader = becSpreader2.dao().findById(member_id);
        if (null != becSpreader) {
            return false;
        }
        BecSpreader becSpreader1 = new BecSpreader();
        becSpreader1.setMemberId(member_id).setName(name).setMobile(mobile).setWhyApply(why_apply).setApplyTime(System.currentTimeMillis());
        return becSpreader1.save();
    }

    /**
     * 获取分销商下线
     * @param id
     * @return
     */
    List<BecMember> spreader_friends(Integer id) {
        Kv cond = Kv.by("spreader_id", id);
        return memberService.getCommonList(cond);
    }



    /**
     * @author zedomi
     */

    /****************
     * 获取分销商信息
     */
    public Page<BecSpreader> spreader_paginate(Integer pageNumber, Integer pageSize, String keywords){
        Kv kv = Kv.by("table",table());
        kv.setIfNotBlank("keywords",keywords);

        return dao().paginate(pageNumber,pageSize, Db.getSqlPara("ec.mall.manage.spreader.select_spreader_paginate",kv));
    }

    /**
     * 拒绝/通过 该用户成为分销商
     */
    public Ret delete_pass_audited(Integer id, Integer audited, String reason){
        if (id!=null){
            BecSpreader becSpreader1 = new BecSpreader();
            BecSpreader becSpreader = becSpreader1.dao().findById(id);
            if (becSpreader!=null){
                becSpreader.setAudited(audited);
                becSpreader.setTurnDownReason(reason);
                becSpreader.setAuditTime(System.currentTimeMillis());
                Boolean success= becSpreader.update();
                if (success){
                    return success("操作成功");
                }
                else {
                    return fail("操作失败");
                }
            }
            else {
                return fail("操作失败");
            }
        }
        return fail("操作失败");



    }


}
